package run.newbug.sso.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class JwtUtils {
    private static String secret = "AAAAABBBBBCCCC";
    public static String generateToken(Map<String,Object> map){
        map.put("username", "DAMN4374");
        map.put("permission", "ROLE_andmin");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 10);
        Date time = calendar.getTime();
        String token = Jwts.builder().setClaims(map)
                .setExpiration(time)
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, secret)
                .compact();
        return token;
    }

    public static Claims getClaimsFromToken(String token){
        Claims body = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
        return body;
    }

    public static boolean isTokenExpired(String token){
        Date expiration = getClaimsFromToken(token).getExpiration();
        return expiration.before(new Date());

    }
}
